//==========================================================================//
//                                                                          //
//  PACHUBE Demo Sketch         Version 1-alpha                 March 2012  //
//                                                                          //
//  Written for the Arduino Duemilanove w/ ATmega328                        //
//                                                                          //
//  PACHUBE Arduino Library Demo Sketch                                     // 
//                                                                          //
//  Created by JFLagarde (jeanfrancoislagarde@hotmail.com)                  //
//                                                                          //
//  This file is part of OpenWS (http://code.google.com/p/openws/)          //
//                                                                          //
//==========================================================================//
//                                                                          //
//  Notes:                                                                  //
//                                                                          //
//                                                                          //
//==========================================================================//
//                                                                          //
//  Copyright 2012 Jean-Francois Lagarde (jeanfrancoislagarde@hotmail.com)  //
//  This program is distributed under the terms of                          //
//  the GNU General Public License                                          //
//                                                                          //
//  This program is free software: you can redistribute it and/or modify    //
//  it under the terms of the GNU General Public License as published by    //
//  the Free Software Foundation, either version 3 of the License, or       //
//  (at your option) any later version.                                     //
//                                                                          //
//  This program is distributed in the hope that it will be useful,         //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of          //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           //
//  GNU General Public License for more details.                            //
//                                                                          //
//  You should have received a copy of the GNU General Public License       //
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.   //
//                                                                          //
//==========================================================================//
#include <Ethernet.h>
#include <SPI.h>
#include <Wire.h>
#include <PACHUBE.h>
#include <ANEMOMETER.h>
//#include <DHT22.h>
#include <SHT11.h>
#include <BMP085.h>
#include <ZXTHERM.h>
#include <Metro.h>

byte mac[]     = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte ip[]      = {192,168,1,54};   // IP address of Ethernet Shield

// Create instances
PACHUBE myPACHUBE = PACHUBE();
ANEMOMETER myANEMOMETER = ANEMOMETER();
//DHT22 myDHT22 = DHT22();
SHT11 mySHT11 = SHT11();
BMP085 myBMP085 = BMP085();
//ZXTHERM myZXTHERM = ZXTHERM();

Metro readMetro = Metro(3000);  // Instantiate an instance
Metro pachubeMetro = Metro(120000);  // Instantiate an instance

void setup(void) { 
  // start serial port
  Serial.begin(9600);
  Serial.println("Arduino Weather Station of Marina de Sainte-Anne-des-Monts");
  Serial.println("See the data feed on http://pachube.com/feeds/51435");
  Serial.println();
  Serial.println();

  analogReference(EXTERNAL);
	
  Wire.begin();
  Ethernet.begin(mac);
  delay(1000);   // give the Ethernet shield a second to initialize
  
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    Ethernet.begin(mac, ip); // Configure manually
  }
	
  // initialize instances
  myPACHUBE.init();
  myANEMOMETER.init(2,0);
  //myDHT22.init(3);
  mySHT11.init(5,6);
  myBMP085.init();
//  myZXTHERM.init(1);
  
    Serial.print("speed\t");
    Serial.print("dir\t");
    Serial.print("mSp\t");
    Serial.print("mDi\t");
    Serial.print("gust\t");
    Serial.print("temp");
    Serial.println();
}

void loop(void) { 
  if (readMetro.check()) {
    myANEMOMETER.read();
  //  myZXTHERM.read();
    
    myANEMOMETER.calcWind();

    
    Serial.print(myANEMOMETER.getWindSpeed());
    Serial.print("\t");
    Serial.print(myANEMOMETER.getWindDirection());
    Serial.print("\t");
    Serial.print(myANEMOMETER.getMeanWindSpeed());
    Serial.print("\t");
    Serial.print(myANEMOMETER.getMeanWindDirection());
    Serial.print("\t");
    Serial.print(myANEMOMETER.getWindGust());
    //Serial.print("\t");
//    Serial.print(myZXTHERM.getTemperature());
    Serial.println();
  }
  
  if (pachubeMetro.check()) {
    //int chk = myDHT22.read();
    //if (chk==0) {}

    mySHT11.read();
    myBMP085.read();
    
    myPACHUBE.send("windspeed", myANEMOMETER.getMeanWindSpeed(), "winddirection", myANEMOMETER.getMeanWindDirection(), "windgust", myANEMOMETER.getWindGust(), "temperature", mySHT11.getTemperature(), "humidity", mySHT11.getHumidity(), "pressure", myBMP085.getPressure());

  }
}
